package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang3.StringUtils;

import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.SimpleJdbcDao;
import com.cardone.platform.authority.dto.NavigationDto;
import com.google.common.collect.Maps;

/**
 * 导航
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class NavigationJdbcDao extends SimpleJdbcDao<NavigationDto> implements NavigationDao {
	public NavigationJdbcDao() {
		this.deleteByIds = SqlIds.deleteByIds.id();

		this.find = SqlIds.find.id();

		this.findByCode = SqlIds.findByCode.id();

		this.findById = SqlIds.findById.id();

		this.findByLikeCode = SqlIds.findByLikeCode.id();

		this.insertByCode = SqlIds.insertByCode.id();

		this.insertByNotExistsCode = SqlIds.insertByNotExistsCode.id();

		this.readByCode = SqlIds.readByCode.id();

		this.readByLikeCode = SqlIds.readByLikeCode.id();

		this.updateByCode = SqlIds.updateByCode.id();
	}

	@Override
	public <P> List<P> findListByParentCode(final Class<P> mappedClass, final String siteCode, final String typeCode, final String parentCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.siteCode.name(), siteCode);

		model.put(Attributes.typeCode.name(), typeCode);

		model.put(Attributes.parentCode.name(), parentCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByParentCode.id(), model);
	}

	@Override
	public <P> List<P> findListByParentId(final Class<P> mappedClass, final String siteCode, final String typeCode, final String parentId) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.siteCode.name(), siteCode);

		model.put(Attributes.typeCode.name(), typeCode);

		model.put(Attributes.parentId.name(), parentId);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByParentId.id(), model);
	}

	@Override
	public boolean isBlankById(final NavigationDto dto) {
		return StringUtils.isBlank(dto.getId());
	}

	@Override
	public void setId(final NavigationDto dto) {
		dto.setId(UUID.randomUUID().toString());
	}
}